<!--
 * @Author: zhoulei0232
 * @Date: 2021-06-17 11:36:05
 * @LastEditTime: 2021-11-15 17:23:26
 * @LastEditors: zhoulei0232
 * @Description: 
-->

<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="loginForm"
      label-width="0px"
      class="login_form"
    >
      <h1 class="login_title">后台系统</h1>
      <el-form-item prop="name">
        <el-input
          type="text"
          v-model="ruleForm.userName"
          placeholder="请输入账号"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.passWord"
          placeholder="请输入密码"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <div class="login_rem">
        <el-checkbox
          label="记住密码 (账号/密码:admin/123456)"
          v-model="remember"
        ></el-checkbox>
      </div>
      <el-form-item>
        <el-button
          class="login_button"
          type="primary"
          @click="submitForm('ruleForm')"
          :loading="logining"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script  lang="ts" >
import { reactive, toRefs, ref } from "vue";
import service from "@/api/index";
import { useStore } from "@/store";
import { useRouter } from "vue-router";

export default {
  name: "login",
  setup() {
    const store = useStore();
    const router = useRouter();

    const loginForm: any = ref(null);
    const state = reactive({
      logining: false,
      ruleForm: {
        userName: "admin",
        passWord: "admin",
      },
      // 错误提示
      rules: {
        userName: [{ required: true, message: "请输入账号", trigger: "blur" }],
        passWord: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      remember: true,
    });
    // 表单提交
    const submitForm = (formName: string) => {
      console.log("sdsdsd", service);

      loginForm.value.validate((valid: boolean) => {
        console.log(11, valid);
        if (valid) {
          console.log(2211, state);
          service
            .login({
              userName: state.ruleForm.userName,
              passWord: state.ruleForm.passWord,
            })
            .then((res: any) => {
              const { data, success, code } = res;
              console.log("撒大苏打实打实", success);
              console.log(res.data);
              if (success) {
                store.dispatch("appModule/UpdateUserInfo", data.userInfo);
                router.push("/dashboard");
              }
            })
            .catch((err: any) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    };

    return {
      ...toRefs(state),
      loginForm,
      submitForm,
    };
  },
};
</script>

<style  lang="scss" scoped >
.login {
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-weight: bold;
  display: flex;
  flex-flow: column nowrap;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-image: url("../../../public/img/login_bg.jpg");
  background-size: 100% 100%;
  &_form {
    -webkit-border-radius: 20px;
    border-radius: 20px;
    -moz-border-radius: 20px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 50px 20px 50px;
    background-image: linear-gradient(
      to right,
      rgba(245, 245, 220, 0.8),
      rgba(255, 255, 224, 0.85)
    );
    border: 1px solid rgba(245, 245, 220, 0.6);
    // box-shadow: 0 0 5px #AFEEEE;
  }
  &_title {
    margin: 0 0 20px 0;
    text-align: center;
    font-size: 1.8rem;
  }
  &_button {
    width: 50%;
  }
  &_rem {
    display: flex;
    flex-flow: row nowrap;
    align-items: flex-start;
    margin-bottom: 20px;
    // border: 1px solid red
  }
}
</style>